package co.chuangdian.oflauncher.setting;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import uc.SubTitleView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import co.chuangdian.oflauncher.OFActivity;
import co.chuangdian.oflauncher.R;
import co.chuangdian.oflauncher.uc.SelectFolderActivity;
import cn.onekit.*;
import cn.onekit.android.ACTIVITY;

public class UnlockScreenActivity extends OFActivity implements OnClickListener {
	JSONArray folders;
	JSONObject setting;
	RadioButton currentfolder_radiobutton;
	RadioButton selectfolder_radiobutton;
	SubTitleView selectFolderView;
	final static String msg_setting_unlockscreen_selectfolder = "setting_unlockscreen_selectfolder";

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_unlockscreen);

		currentfolder_radiobutton = (RadioButton) findViewById(R.id.currentfolder_radiobutton);
		selectfolder_radiobutton = (RadioButton) findViewById(R.id.selectfolder_radiobutton);
		selectFolderView=(SubTitleView)findViewById(R.id.selectFolderView);
		//
		this.findViewById(R.id.currentfolderRow).setOnClickListener(this);
		this.findViewById(R.id.selectfolderRow).setOnClickListener(this);
		//
		MESSAGE.receive(msg_setting_unlockscreen_selectfolder,null,
				new CALLBACK<Bundle>() {

					@Override
					public void run(boolean isError, Bundle result) {
						int folder_id = result.getInt("folder_id");
						String settingString = (String) CONFIG.get("setting");
						setting = JSON.parse(settingString);
						try {
							setting.put("unlockscreen", folder_id);
							CONFIG.set("setting", setting);
							showUnlockscreenFolder();
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

				});
		//
		String foldersString = (String) CONFIG.get("folders");
		folders = JSON.parses(foldersString);
		showUnlockscreenFolder();

	}

	void showUnlockscreenFolder() {
		String settingString = (String) CONFIG.get("setting");
		setting = JSON.parse(settingString);
		int unlockscreen = setting.optInt("unlockscreen");
		currentfolder_radiobutton.setChecked(unlockscreen == 0);
		selectfolder_radiobutton.setChecked(unlockscreen > 0);
		//
		selectFolderView.setSubtitle("");
		for(int i=0;i<folders.length();i++){
			JSONObject folder;
			try {
				folder = folders.getJSONObject(i);
				if(folder.optInt("id")==unlockscreen){
					selectFolderView.setSubtitle(folder.optString("name"));
					break;
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.currentfolderRow:
			try {
				setting.put("unlockscreen", 0);
				 CONFIG.set("setting",setting);
				 showUnlockscreenFolder();
			} catch (JSONException e) {
				e.printStackTrace();
			}
			 
			break;
		case R.id.selectfolderRow:
			int unlockscreen = setting.optInt("unlockscreen");
			Intent intent = new Intent(this, SelectFolderActivity.class);
			intent.putExtra("current_id", unlockscreen);
			intent.putExtra("message_id", msg_setting_unlockscreen_selectfolder);
			startActivity(intent);
			break;

		}

	}

}